/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_version.service;


import com.yuecai.modules.yc_version.domain.YcVersion;
import com.yuecai.modules.yc_version.repository.YcVersionRepository;
import com.yuecai.modules.yc_version.service.dto.YcVersionDto;
import com.yuecai.modules.yc_version.service.dto.YcVersionQueryCriteria;
import com.yuecai.modules.yc_version.service.mapstruct.YcVersionMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yc_version
* @date 2020-07-08
**/
@Service
@RequiredArgsConstructor
public class YcVersionService {

    private final YcVersionRepository ycVersionRepository;
    private final YcVersionMapper ycVersionMapper;

    
    public Map<String,Object> queryAll(YcVersionQueryCriteria criteria, Pageable pageable){
        Page<YcVersion> page = ycVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycVersionMapper::toDto));
    }

    
    public List<YcVersionDto> queryAll(YcVersionQueryCriteria criteria){
        return ycVersionMapper.toDto(ycVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcVersionDto findById(Long id) {
        YcVersion ycVersion = ycVersionRepository.findById(id).orElseGet(YcVersion::new);
        ValidationUtil.isNull(ycVersion.getId(),"YcVersion","id",id);
        return ycVersionMapper.toDto(ycVersion);
    }


    @Transactional
    public YcVersionDto findByLimit1(String platefrom) {
        YcVersion firstByOrderByCreateTime = ycVersionRepository.findByPlatfromAndFirstByOrderByCreateTimeDesc(platefrom);
        return ycVersionMapper.toDto(firstByOrderByCreateTime);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcVersionDto create(YcVersion resources) {
        return ycVersionMapper.toDto(ycVersionRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcVersion resources) {
        YcVersion ycVersion = ycVersionRepository.findById(resources.getId()).orElseGet(YcVersion::new);
        ValidationUtil.isNull( ycVersion.getId(),"YcVersion","id",resources.getId());
        ycVersion.copy(resources);
        ycVersionRepository.save(ycVersion);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycVersionRepository.deleteById(id);
        }
    }

    
    public void download(List<YcVersionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcVersionDto ycVersion : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("版本号", ycVersion.getVersionCode());
            map.put("下载地址", ycVersion.getDownloadUrl());
            map.put("版本简介", ycVersion.getVersionInfo());
            map.put("版本名称", ycVersion.getVersionName());
            map.put(" createTime",  ycVersion.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}